1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.google.common.collect;
17  
18  import static com.google.common.base.Preconditions.checkNotNull;
19  
20  import com.google.common.annotations.GwtCompatible;
21  
22  import java.util.Comparator;
23  
24  import javax.annotation.Nullable;
25  
26  /**
27   * An empty immutable sorted map.
28   *
29   * @author Louis Wasserman
30   */
31  @GwtCompatible(emulated = true)
32  @SuppressWarnings("serial") // uses writeReplace, not default serialization
33  final class EmptyImmutableSortedMap<K, V> extends ImmutableSortedMap<K, V> {
34    private final transient ImmutableSortedSet<K> keySet;
35  
36    EmptyImmutableSortedMap(Comparator<? super K> comparator) {
37      this.keySet = ImmutableSortedSet.emptySet(comparator);
38    }
39  
40    EmptyImmutableSortedMap(
41        Comparator<? super K> comparator, ImmutableSortedMap<K, V> descendingMap) {
42      super(descendingMap);
43      this.keySet = ImmutableSortedSet.emptySet(comparator);
44    }
45  
46    @Override
47    public V get(@Nullable Object key) {
48      return null;
49    }
50  
51    @Override
52    public ImmutableSortedSet<K> keySet() {
53      return keySet;
54    }
55  
56    @Override
57    public int size() {
58      return 0;
59    }
60  
61    @Override
62    public boolean isEmpty() {
63      return true;
64    }
65  
66    @Override
67    public ImmutableCollection<V> values() {
68      return ImmutableList.of();
69    }
70  
71    @Override
72    public String toString() {
73      return "{}";
74    }
75  
76    @Override
77    boolean isPartialView() {
78      return false;
79    }
80  
81    @Override
82    public ImmutableSet<Entry<K, V>> entrySet() {
83      return ImmutableSet.of();
84    }
85  
86    @Override
87    ImmutableSet<Entry<K, V>> createEntrySet() {
88      throw new AssertionError("should never be called");
89    }
90  
91    @Override
92    public ImmutableSetMultimap<K, V> asMultimap() {
93      return ImmutableSetMultimap.of();
94    }
95  
96    @Override
97    public ImmutableSortedMap<K, V> headMap(K toKey, boolean inclusive) {
98      checkNotNull(toKey);
99      return this;
100   }
101 
102   @Override
103   public ImmutableSortedMap<K, V> tailMap(K fromKey, boolean inclusive) {
104     checkNotNull(fromKey);
105     return this;
106   }
107 
108   @Override
109   ImmutableSortedMap<K, V> createDescendingMap() {
110     return new EmptyImmutableSortedMap<K, V>(Ordering.from(comparator()).reverse(), this);
111   }
112 }